using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace SPLogViewer
{
    public partial class frmFilter : Form
    {

        Point _formStartPoint;
        public List<string> ProcessFilter;
        
        public frmFilter()
        {
            InitializeComponent();
        }

     
        public void SetLocation(Point location)
        {
            _formStartPoint = location;
        }

        private void frmFilter_Load(object sender, EventArgs e)
        {
            Location = _formStartPoint;
        }

        public void SetFilter(DataTable dt, string filterColumn)
        {
            DataRow[] distinctRows = null;
            lock (dt)
            {
                this.Text = "Filter for: " + filterColumn;
                distinctRows = GetDistinctRows(dt, filterColumn);
            }
            if (distinctRows != null)
            {
                List<string> distinctItems = new List<string>();
                foreach (DataRow row in distinctRows)
                {
                    distinctItems.Add(row[filterColumn].ToString());
                }
                distinctItems.Sort();

                foreach (string str in distinctItems)
                {
                    int index = chkFilter.Items.Add(str);
                    if (ProcessFilter.Contains(str))
                    {
                        chkFilter.SetItemChecked(index, true);
                    }
                }
            }
            

        }

        private DataRow[] GetDistinctRows(DataTable drows, string colName)
        {            
            
            DataRow[] objArray = null;
            if (drows != null && colName != null && colName != string.Empty)
            {
                Hashtable hTable = new Hashtable();
                List<DataRow> distinctRows = new List<DataRow>();

                foreach (DataRow drow in drows.Rows)
                    if (!hTable.ContainsKey(drow[colName].ToString()))
                    {
                        hTable.Add(drow[colName].ToString(), string.Empty);
                        distinctRows.Add(drow);
                    }
                objArray = new DataRow[distinctRows.Count];
                distinctRows.CopyTo(objArray);
            }
            return objArray;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}